<?php

namespace App\Http\Controllers\Guard\User;

use App\Http\Controllers\Common\ApiController;
use App\Models\User\FlyOrder;
use App\Models\User\PointLog;
use App\Models\User\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class FlyOrderController extends ApiController {


    public function __construct() {
        $this->instance = new FlyOrder();
    }

    public function check(Request $request) {
        $id = $request->input('id', 0);
        $need_point = $request->input('need_point', 0);

        try {

            DB::beginTransaction();

            //审核订单
            $record = $this->instance->newQuery()->find($id);
            $record->need_point = $need_point;
            $record->status = 2;
            $record->save();

            //用户加分
            $user = User::query()->find($record->user_id);
            $user->point = $user->point + $need_point;
            $user->save();

            //积分log
            PointLog::query()->create([
                'user_id' => $record->user_id,
                'name' => $user->name,
                'passport' => $user->passport,
                'phone' => $user->phone,
                'action' => 'add',
                'point' => $record->need_point,
                'reason' => '机票申请',
            ]);

            DB::commit();
            return responseSuccess();
        } catch (\Exception $e) {
            DB::rollBack();
            $tip = $e->getFile() . ':' . $e->getLine() . ':' . $e->getMessage();
            return responseFail($tip);
        }
    }

    public function lst(Request $request) {
        $page = $request->input('page', 1);
        $pageSize = $request->input('pageSize', 50);
        $conditions = $request->input();
        $records = $this->instance->getLst($conditions)
            ->with('getUser')
            ->orderBy('status')
            ->paginate($pageSize, $this->getCol, 'page', $page);
        return responseSuccess($records->items(), $records->total());

    }

    public function add(Request $request) {
        $info = $request->input();

        $this->validate($request, [
            'userId' => 'required|Array',
            'bookingDate' => 'required',
            'flyNumber' => 'required',
            'needPoint' => 'required|Integer|gt:0',

        ], [], [
            'userId' => '用户',
            'flyNumber' => '航班号',
            'needPoint' => '积分',
            'bookingDate' => '订票时间',
        ]);

        try {
            DB::beginTransaction();
            foreach ($info['userId'] as $userId) {

                //添加订单
                $this->instance->newQuery()->create([
                    'user_id' => $userId,
                    'fly_number' => $info['flyNumber'],
                    'need_point' => $info['needPoint'],
                    'booking_date' => $info['bookingDate'],
                    'status' => 2,
                    'type' => 2
                ]);

                //拥挤加积分
                $user = User::query()->find($userId);
                $user->point += $info['needPoint'];
                $user->save();

                //积分log
                PointLog::query()->create(['user_id' => $user->id,
                    'name' => $user->name,
                    'phone' => $user->phone,
                    'passport' => $user->passport,
                    'action' => 'add',
                    'point' => $info['needPoint'],
                    'reason' => '后台手动创建订单加分'
                ]);

            }
            DB::commit();
            return responseSuccess();
        } catch (\Exception $e) {
            DB::rollBack();
            $tip = $e->getFile() . ':' . $e->getLine() . ':' . $e->getMessage();
            return responseFail($tip);
        }
    }
}
